package com.pengtu.gsj.service.impl.yh;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.yh.RoadRepairDao;
import com.pengtu.gsj.entity.app.Organization;
import com.pengtu.gsj.entity.yh.RoadRepair;
import com.pengtu.gsj.service.system.OrganizationService;
import com.pengtu.gsj.service.yh.RoadRepairService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.encode.JsonBinder;

@Transactional
@Service
public class RoadRepairServiceImpl implements RoadRepairService{
	
	@Autowired
	private RoadRepairDao roadRepairDao;
	
	@Autowired
	private OrganizationService organizationService;

	@Override
	public RoadRepair getRoadRepairById(String id) {
		return roadRepairDao.get(id);
	}

	@Override
	public void saveRoadRepair(RoadRepair roadRepair) {
		roadRepairDao.save(roadRepair);
	}

	/**
	 * 同步小修系统中的小修项目数据
	 */
	@Override
	public void dockingRoadRepair() throws Exception {
		Date time = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = sdf.format(time);
		String lastTime = roadRepairDao.getMaxDate();
		String urlString = ConfigUtils.getConfig("repairAddress");
		// 如果从文件中读取的URL地址最后一个字符不是 '\'，则添加'\'    
        if (urlString.charAt(urlString.length() - 1) != '/') {    
        	urlString += "/";    
        } 
        urlString+="gsj/repair.action?";
		if(StringUtils.isNotBlank(lastTime)){
			urlString += "beginTime="+URLEncoder.encode(lastTime,"utf-8");
		}
		if(StringUtils.isNotBlank(currentTime)){
			urlString += "&endTime="+URLEncoder.encode(currentTime,"utf-8");
		}
		URL url;
		System.out.println("url:"+urlString);
		StringBuffer st=new StringBuffer();
		url = new URL(urlString);
		URLConnection urlData=url.openConnection();
		urlData.setConnectTimeout(30000);
		BufferedReader reader =new BufferedReader(new InputStreamReader(urlData.getInputStream(),"utf-8"));
		String line=null;
		while((line=reader.readLine())!=null){
			st.append(line);
		}
		String sts=st.toString();
		JsonBinder binder=new JsonBinder(Inclusion.ALWAYS);
		List<RoadRepair> list = binder.fromJsonList(sts, new TypeReference<List<RoadRepair>>() {});
		if(list!=null){
			for(RoadRepair entity :list){
				if (StringUtils.isNotEmpty(entity.getMaintainOrgId())) {
					Organization mainOrganization = organizationService.getOrganizationByDockId(entity.getMaintainOrgId());
					if (mainOrganization != null) {
						entity.setMainOrganization(mainOrganization);
						entity.setMaintainOrg(mainOrganization.getOrgName());
					}
				}
				roadRepairDao.saveOnly(entity);
			}
		}
		
	}

	@Override
	public Page<RoadRepair> getRoadRepairList(Page<RoadRepair> page,
			QueryFilter filters) {
		return roadRepairDao.findPage(page, filters);
	}

}
